<template>
    <div class="content">
        <OverReasonFollowSearch @handle-btn="handleBtn" ref="risk-search" @comReturn="value => (isOpen = value)" />
        <el-divider></el-divider>
        <div style="margin: 10px; color: #575757">统计区间：{{ dateFormat(dateRange.startDate) }}~{{ dateFormat(dateRange.endDate) }}</div>
        <RiskTableP
            ref="risk-table"
            :tableConfig="tableConfig"
            :tableData="tableData"
            :column="tableColumns"
            :paging="paging"
            @page-change="getTableData"
            class="table"
            :style="{ height: isOpen ? 'calc(100% - 140px)' : 'calc(100% - 100px)' }"
            v-loading="tableLoading"
        >
            <template #dictStockCodes="{ row }">
                <span :key="fieldKey + 'dictStockCodes'">
                    <el-tooltip placement="bottom" popper-class="tips-popper">
                        <div slot="content" v-if="row.hasOwnProperty('dictStockCodes') && row.dictStockCodes.length > 1">
                            <div v-for="(item, index) in row.dictStockCodes" :key="index">
                                {{ item.dictKey + '(' + item.dictKeyName + ')' }}
                            </div>
                        </div>

                        <el-tag v-if="row.hasOwnProperty('dictStockCodes') && row.dictStockCodes.length > 1" style="cursor: default">
                            {{ row.dictStockCodes.length + '个证券' }}</el-tag
                        >
                    </el-tooltip>
                    <span v-if="row.hasOwnProperty('dictStockCodes') && row.dictStockCodes.length === 1">
                        {{ row.dictStockCodes[0].dictKey + '(' + row.dictStockCodes[0].dictKeyName + ')' }}
                    </span>
                    <span v-if="!row.hasOwnProperty('dictStockCodes') || (row.hasOwnProperty('dictStockCodes') && row.dictStockCodes.length === 0)">
                        --
                    </span>
                </span>
            </template>
            <template #operation="{ row }">
                <RiskIconButton type="primary" topTitle="超标原因" icon="el-icon-view" @on-click="handleRowBtn('over-reason', row)"></RiskIconButton>
                <RiskIconButton
                    type="primary"
                    topTitle="超标跟踪"
                    icon="el-icon-data-analysis"
                    @on-click="handleRowBtn('over-follow', row)"
                ></RiskIconButton>
            </template>
        </RiskTableP>
    </div>
</template>
<script lang="ts">
import RiskTableP from '@/common/components/risk-table-plus.vue';
import { PageVO, TableConfigVO } from '@/common/vo';
import { dateFormat, numberFormat } from '@/utils/tools';
import { Component, Vue } from 'vue-property-decorator';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import { mixins } from 'vue-class-component';
import API from '@/pages/static/api/over-reason-follow';
import OverReasonFollowSearch from './over-reason-follow-search.vue';
import { overReasonFollowVO } from '../vo/risk-result';
import RiskIconButton from '@/common/components/risk-icon-button.vue';
import DictionConstUtil from '@/utils/diction-const-util';
import { downLoadFile } from '@/utils/request';
import XEUtils from 'xe-utils';
@Component({ name: 'OverReasonFollowList', components: { RiskTableP, OverReasonFollowSearch, RiskIconButton } })
export default class OverReasonFollowList extends mixins(RiskEmitter) {
    numberFormat = numberFormat;
    dateFormat = dateFormat;
    isOpen: boolean = false;
    fieldKey: number = -1;
    tableConfig: TableConfigVO = {
        rowKey: 'recordId', // 主键key
        allRowKeys: [], // 所有页数据的key集合
    };
    // overReasonFollowVO
    tableData: any[] = [
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '469',
                    dictKeyName: '赢和469号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [
                {
                    dictKey: '143173 SS',
                    dictKeyName: '17广药01',
                    existsMarginRate: false,
                    param2: '5688',
                    precision: 0,
                },
            ],
            entryCode: '1553',
            entryKey: '47:1553:6:469:5688-47:1553:12:0:0',
            entryKey2: '1553:469:5688',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '469',
            fundCodes: ['469'],
            fundName: '赢和469号',
            fundType: '',
            overrunName: '主动-基金经理主动操作-3.1022%',
            result: 0.52380952,
            resultId: 1530003016,
            stockCode: '143173',
            stockCodes: ['143173 SS'],
            stockName: '17广药01',
            tradeDate: 20230821,
            triggerType: '3',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '7010',
                    dictKeyName: '赢和7号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:7010:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '7010',
            fundCodes: ['7010'],
            fundName: '赢和7号',
            fundType: '',
            result: 0,
            resultId: 1530003017,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '3903',
                    dictKeyName: '赢和3号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:3903:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '3903',
            fundCodes: ['3903'],
            fundName: '赢和3号',
            fundType: '',
            result: 0,
            resultId: 1530003018,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '469',
                    dictKeyName: '赢和469号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [
                {
                    dictKey: '136529 SS',
                    dictKeyName: '16中车G3',
                    existsMarginRate: false,
                    param2: '3554',
                    precision: 0,
                },
            ],
            entryCode: '1553',
            entryKey: '47:1553:6:469:1533-47:1553:12:0:0',
            entryKey2: '1553:469:1533',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '469',
            fundCodes: ['469'],
            fundName: '赢和469号',
            fundType: '',
            result: 0.47619048,
            resultId: 1530003019,
            stockCode: '136529',
            stockCodes: ['136529 SS'],
            stockName: '16中车G3',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: true,
            adjustDays: 0,
            dictFundCodes: [
                {
                    dictKey: '2316',
                    dictKeyName: '赢和5号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:2316:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230818,
            firstRestoreDate: 20230821,
            fundCode: '2316',
            fundCodes: ['2316'],
            fundName: '赢和5号',
            fundType: '',
            result: 0,
            resultId: 1530003020,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '3901',
                    dictKeyName: '赢和1号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:3901:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '3901',
            fundCodes: ['3901'],
            fundName: '赢和1号',
            fundType: '',
            result: 0,
            resultId: 1530003021,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '2317',
                    dictKeyName: '赢和6号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:2317:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '2317',
            fundCodes: ['2317'],
            fundName: '赢和6号',
            fundType: '',
            result: 0,
            resultId: 1530003022,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '2904',
                    dictKeyName: '赢和4号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:2904:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '2904',
            fundCodes: ['2904'],
            fundName: '赢和4号',
            fundType: '',
            result: 0,
            resultId: 1530003023,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '3902',
                    dictKeyName: '赢和2号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:3902:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '3902',
            fundCodes: ['3902'],
            fundName: '赢和2号',
            fundType: '',
            result: 0,
            resultId: 1530003024,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '2999',
                    dictKeyName: '赢和8号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [],
            entryCode: '1553',
            entryKey: '47:1553:6:2999:0-47:1553:12:0:0',
            entryName: '控转回质押导致的被动超标',
            firstOverrunDate: 20230821,
            fundCode: '2999',
            fundCodes: ['2999'],
            fundName: '赢和8号',
            fundType: '',
            result: 0,
            resultId: 1530003025,
            stockCode: '',
            stockCodes: [],
            stockName: '',
            tradeDate: 20230821,
            triggerType: '0',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '469',
                    dictKeyName: '赢和469号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [
                {
                    dictKey: '143173 SS',
                    dictKeyName: '17广药01',
                    existsMarginRate: false,
                    param2: '5688',
                    precision: 0,
                },
            ],
            entryCode: '1553',
            entryKey: '47:1553:6:469:5688-47:1553:12:0:0',
            entryKey2: '1553:469:5688',
            entryName: '控转回质押导致的被动超标1',
            firstOverrunDate: 20230820,
            fundCode: '469',
            fundCodes: ['469'],
            fundName: '赢和469号',
            fundType: '',
            result: 1.52380952,
            resultId: 15310030171,
            stockCode: '143173',
            stockCodes: ['143173 SS'],
            stockName: '17广药01',
            tradeDate: 20230821,
            triggerType: '3',
        },
        {
            adjust: false,
            dictFundCodes: [
                {
                    dictKey: '469',
                    dictKeyName: '赢和469号',
                    existsMarginRate: false,
                    precision: 0,
                },
            ],
            dictStockCodes: [
                {
                    dictKey: '143173 SS',
                    dictKeyName: '17广药01',
                    existsMarginRate: false,
                    param2: '5688',
                    precision: 0,
                },
            ],
            entryCode: '1553',
            entryKey: '47:1553:6:469:5688-47:1553:12:0:0',
            entryKey2: '1553:469:5688',
            entryName: '控转回质押导致的被动超标2',
            firstOverrunDate: 20230820,
            fundCode: '469',
            fundCodes: ['469'],
            fundName: '赢和469号',
            fundType: '',
            result: 1.52380952,
            resultId: 15310030211,
            stockCode: '143173',
            stockCodes: ['143173 SS'],
            stockName: '17广药01',
            tradeDate: 20230821,
            triggerType: '3',
        },
    ];
    tableColumns = [
        // { field: 'fundCode', title: '产品代码', minWidth: '100px' },
        // { field: 'fundName', title: '产品名称', minWidth: '100px' },
        {
            field: 'dictFundCodes',
            title: '产品代码(名称)',
            minWidth: 160,
            render: value => this.formatDictFundCodes(value),
        },
        { field: 'fundType', title: '产品类型', minWidth: '100px' },
        { field: 'entryCode', title: '条目编号', minWidth: '100px' },
        { field: 'entryName', title: '风险说明', minWidth: '150px' },
        // { field: 'stockCode', title: '证券代码', minWidth: '100px' },
        // { field: 'stockName', title: '证券名称', minWidth: '130px' },
        {
            field: 'dictStockCodes',
            title: '证券代码(名称)',
            minWidth: 140,
        },
        { field: 'thresholdValue', title: '阈值', minWidth: '100px' },
        { field: 'firstOverrunDate', title: '超标日期', minWidth: '100px', render: value => dateFormat(value) },
        { field: 'overrunName', title: '超标原因', minWidth: '130px' },
        {
            field: 'adjust',
            title: '是否已调整',
            minWidth: '100px',
            render: value => {
                return value ? '是' : '否';
            },
        },
        { field: 'adjustDate', title: '调整日期', minWidth: '100px' },
        { field: 'adjustDays', title: '调整天数', minWidth: '100px' },
        { field: 'restoreName', title: '调整跟踪', minWidth: '150px' },
        {
            title: '操作',
            field: 'operation',
            fixed: 'right',
            width: 120,
            align: 'center',
        },
    ];
    paging: PageVO = {
        currentPage: 1,
        pageSize: 25,
        total: 0,
    };

    // 符号字典
    private CompareDirection = {
        '0': '>',
        '1': '<',
        '2': '>=',
        '3': '<=',
        '4': '=',
        '5': '<>',
        '6': ' ',
    };

    // 颜色字典
    private ThresholadEntryColor = {
        '0': '#8bd197',
        '1': '#ff962c',
        '2': '#1d80ff',
        '3': '#df3a39',
        '4': '#6d5d5b',
    };
    tableLoading: boolean = false;
    tableKey: number = 0; // 用于重置表头条件
    // 统计区间
    dateRange = {
        startDate: '',
        endDate: '',
    };
    async mounted() {
        await DictionConstUtil.FNTradeDirection();
        this.getTableData();
    }

    getTableData(seatchBtn: boolean = false) {
        let params = JSON.parse(JSON.stringify((this.$refs as any)['risk-search'].searchParams));
        this.dateRange.startDate = params.startDate;
        this.dateRange.endDate = params.endDate;
        this.tableLoading = true;
        API.overReasonFollowList({ ...params, paging: this.paging })
            .then((res: any) => {
                this.tableData = res.list;
                this.paging.total = res.total;
                this.fieldKey++;
                console.log(res, 'res');
                seatchBtn && this.yh_message_success('查询成功');
            })
            .catch((resp: any) => {})
            .finally(() => {
                this.tableLoading = false;
            });
    }

    /**
     * 查看明细
     */
    openEntryResultDetail(row: any) {
        let row1 = JSON.parse(JSON.stringify(row));
        delete row1.resultId;

        let currentEntryInfo = {
            entryCode: row.entryCode,
            fundCodes: row.fundCodes,
            row: row1,
        };
        this.dispatch('ItemRealTimeMonitoring', 'handleEntryDetail', currentEntryInfo);
    }

    /**
     * 按钮点击
     */
    async handleBtn(type: string) {
        if (type == 'search') {
            this.paging.currentPage = 1;
            this.getTableData(true);
        } else if (type == 'loadout') {
            let params = JSON.parse(JSON.stringify((this.$refs as any)['risk-search'].searchParams));
            downLoadFile({
                url: '/ovattr/oucher/summary/export',
                method: 'post',
                data: params,
            });
        }
    }

    /**
     *  获取对象长度
     */
    getObjectLength(obj: any) {
        if (obj) {
            return Object.keys(obj).length;
        } else {
            return 0;
        }
    }

    handleRowClick(row) {
        const msg = {
            type: 'look-entry-details',
            path: `/lookEntryDetails?entryCode=${row.entryCode}`,
            title: `条目详情-${row.entryCode}`,
        };
        top.window.postMessage(msg, '*');
    }

    // 详情
    private handleRowBtn(type, row): void {
        this.$emit('handleRowBtn', { type, row });
    }

    // 格式化产品代码
    private formatDictFundCodes(cellValue): string {
        let valueStr = XEUtils.map(cellValue, item => {
            if (item) {
                return item.dictKey + '(' + item.dictKeyName + ')';
            } else {
                return '--';
            }
        });
        return valueStr.toString();
    }
}
</script>
<style lang="scss" scoped>
.content {
    height: calc(100vh - 47px);
}

.text-hover {
    cursor: pointer;

    &:hover {
        color: var(--primary);
        text-decoration: var(--primary) solid underline;
    }
}
</style>
<style>
.el-popover {
    padding: 10px;
    max-height: 200px;
    overflow: auto;
    background: #333;
    color: #fff;
}

.tips-popper {
    max-width: 700px;
    max-height: 300px;
    overflow: auto;
}
</style>
